<!-- 设备管理-设备维修弹出框组件 -->
<template>
  <a-modal
    unmount-on-close
    width="50%"
    :visible="modalVisible"
    @ok="handleOk"
    @cancel="handleCancel"
    :mask-closable="false"
    :footer="type !== 2 && type !== 5 && type !== 12"
  >
    <template #title>
      {{
        type === 1
          ? '新增设备维修单'
          : type === 2
          ? '故障信息'
          : type === 3
          ? '新建维修任务'
          : type === 4
          ? '维修信息'
          : type === 5 || type === 14 || type === 15
          ? '设备维修详情'
          : type === 6
          ? '新建设备保养单'
          : type === 7
          ? '新建保养任务'
          : type === 8
          ? '保养内容'
          : type === 9
          ? '设备保养详情'
          : type === 10
          ? '新增保养计划'
          : type === 11
          ? '编辑保养计划'
          : '保养计划详情'
      }}
    </template>
    <div style="height: 500px; overflow;: auto">
      <div class="content">
        <div
          v-if="
            type === 1 ||
            type === 2 ||
            type === 3 ||
            type === 5 ||
            type === 14 ||
            type === 15
          "
        >
          <h3>故障信息</h3>
          <a-form auto-label-width :model="currForm.list" direction="vertical">
            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-item field="number" label="维修单号：">
                  <a-input
                    placeholder="系统自动生成"
                    size="large"
                    v-model="currForm.list.number"
                    :disabled="
                      type === 1 ||
                      type === 3 ||
                      type === 2 ||
                      type === 5 ||
                      type === 14 ||
                      type === 15
                    "
                  />
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item field="equipNumber" label="设备编码：" required>
                  <a-input
                    placeholder="请输入设备编码"
                    size="large"
                    v-model="currForm.list.equipNumber"
                    @change="changeEquipNumber"
                    :disabled="
                      type === 3 ||
                      type === 2 ||
                      type === 5 ||
                      type === 14 ||
                      type === 15
                    "
                  />
                </a-form-item>
              </a-col>
            </a-row>

            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-item field="equipName" label="设备名称：">
                  <a-input
                    placeholder="根据设备编号，自动读取设备名称"
                    size="large"
                    v-model="currForm.list.equipName"
                    disabled
                  />
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item field="customerEmail" label="所属车间：">
                  <a-input
                    placeholder="根据设备编号，自动读取所属车间"
                    size="large"
                    v-model="currForm.list.workshop"
                    disabled
                  />
                </a-form-item>
              </a-col>
            </a-row>

            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-item field="reportTime" label="报修时间：">
                  <a-date-picker
                    style="width: 100%"
                    v-model="currForm.list.reportTime"
                    show-time
                    format="YYYY-MM-DD HH:mm:ss"
                    placeholder="请选择时间"
                    value-format="x"
                    disabled
                  />
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item field="customerEmail" label="报修人：">
                  <a-input
                    placeholder="系统自动读取"
                    size="large"
                    v-model="currForm.list.reporterName"
                    disabled
                  />
                </a-form-item>
              </a-col>
            </a-row>

            <a-row :gutter="16">
              <a-col :span="12" class="dept">
                <a-form-item field="description" label="故障描述：">
                  <a-textarea
                    placeholder="请输入"
                    allow-clear
                    show-word-limit
                    :auto-size="{
                      minRows: 5,
                    }"
                    :max-length="300"
                    v-model="currForm.list.description"
                    :disabled="
                      type === 2 || type === 5 || type === 14 || type === 15
                    "
                  />
                </a-form-item>
              </a-col>
            </a-row>

            <a-row :gutter="16">
              <a-col :span="12" class="upload">
                <a-form-item field="customerContactPhone" label="图片上传：">
                  <a-upload
                    :show-remove-button="type === 1"
                    :default-file-list="currForm.list.equipmentRepairPicture"
                    draggable
                    list-type="text"
                    download
                    name="data"
                    @success="uploadFn"
                    :headers="{ Authorization: `${getToken()}` }"
                    :action="url"
                    :disabled="
                      type === 2 || type === 5 || type === 14 || type === 15
                    "
                  />
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <div
          v-if="
            type === 3 || type === 4 || type === 5 || type === 14 || type === 15
          "
        >
          <h3>维修信息</h3>
          <!-- form表单设置auto-label-width开启自动标签宽度 -->
          <a-form :model="currForm.list" direction="vertical" auto-label-width>
            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-item field="repairerId" label="维修人：">
                  <a-input
                    v-if="type === 5 || type === 14 || type === 15"
                    disabled
                    v-model="currForm.list.repairerName"
                  ></a-input>
                  <a-select
                    placeholder="请选择人员"
                    allow-search
                    v-model="currForm.list.repairerId"
                    :options="personnelData.data"
                    :field-names="{
                      value: 'id',
                      label: 'name',
                    }"
                    v-if="type !== 5 && type !== 14 && type !== 15"
                  >
                    <!-- <a-option>Beijing</a-option>
                    <a-option>Shanghai</a-option>
                    <a-option>Guangzhou</a-option>
                    <a-option disabled>Disabled</a-option>
                    <a-option>Shenzhen</a-option>
                    <a-option>Chengdu</a-option>
                    <a-option>Wuhan</a-option> -->
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <!-- <a-form-item field="repairState" label="维修状态：">
                  <a-select
                    placeholder="请选择维修状态"
                    allow-search
                    v-model="currForm.list.repairState"
                    :disabled="type === 5 || type === 15"
                  >
                    <a-option :value="1" v-if="type === 5 || type === 3"
                      >待派工</a-option
                    >
                    <a-option
                      :value="2"
                      v-if="
                        type === 3 ||
                        type === 4 ||
                        type === 5 ||
                        type === 14 ||
                        type === 15
                      "
                      >待维修</a-option
                    >
                    <a-option
                      :value="3"
                      v-if="type === 4 || type === 14 || type === 15"
                      >进行中</a-option
                    >
                    <a-option :value="4">需委外</a-option>
                  <a-option :value="5">已委外</a-option>
                    <a-option
                      :value="6"
                      v-if="type === 5 || type === 4 || type === 15"
                      >已完工</a-option
                    >
                  </a-select>
                </a-form-item> -->
              </a-col>
            </a-row>
            <div v-if="type === 5 || type === 4 || type === 14 || type === 15">
              <a-row :gutter="16">
                <a-col :span="12">
                  <a-form-item field="repairBeginTime" label="维修开始时间：">
                    <a-date-picker
                      style="width: 100%"
                      show-time
                      format="YYYY-MM-DD HH:mm:ss"
                      v-model="currForm.list.repairBeginTime"
                      value-format="x"
                      :disabled="type === 5"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="12">
                  <a-form-item field="repairEndTime" label="维修结束时间：">
                    <a-date-picker
                      style="width: 100%"
                      show-time
                      format="YYYY-MM-DD HH:mm:ss"
                      v-model="currForm.list.repairEndTime"
                      value-format="x"
                      :disabled="type === 5 || type === 14"
                    />
                  </a-form-item>
                </a-col>
              </a-row>
              <a-row :gutter="16">
                <a-col :span="12">
                  <a-form-item field="isReplaceparts" label="是否更换备件：">
                    <a-radio-group
                      :default-value="2"
                      v-model="currForm.list.isReplaceparts"
                      :disabled="type === 5 || type === 14"
                    >
                      <a-radio :value="1">是</a-radio>
                      <a-radio :value="2">否</a-radio>
                    </a-radio-group>
                  </a-form-item>
                </a-col>
                <a-col :span="12">
                  <!-- <a-form-item
                    field="isSubcontracting"
                    label="是否需要委外维修："
                  >
                    <a-radio-group
                      :default-value="2"
                      v-model="currForm.list.isSubcontracting"
                      :disabled="type === 5 || type === 14"
                    >
                      <a-radio :value="1">是</a-radio>
                      <a-radio :value="2">否</a-radio>
                    </a-radio-group>
                  </a-form-item> -->
                </a-col>
              </a-row>
              <a-row :gutter="16">
                <a-col :span="12" class="dept">
                  <a-form-item field="faultAnalysis" label="故障分析：">
                    <a-textarea
                      placeholder="请输入"
                      allow-clear
                      show-word-limit
                      :auto-size="{
                        minRows: 5,
                      }"
                      v-model="currForm.list.faultAnalysis"
                      :disabled="type === 5 || type === 14"
                    />
                  </a-form-item>
                </a-col>
              </a-row>
              <a-row :gutter="16">
                <a-col :span="12" class="dept">
                  <a-form-item field="treatmentMeasures" label="故障处理措施：">
                    <a-textarea
                      placeholder="请输入"
                      allow-clear
                      show-word-limit
                      :auto-size="{
                        minRows: 5,
                      }"
                      v-model="currForm.list.treatmentMeasures"
                      :disabled="type === 5 || type === 14"
                    />
                  </a-form-item>
                </a-col>
              </a-row>
            </div>
          </a-form>
        </div>
        <div v-if="type === 6 || type === 7 || type === 9">
          <h3>保养内容</h3>
          <!-- form表单设置auto-label-width开启自动标签宽度 -->
          <a-form
            :model="currForm.list"
            ref="formRules"
            direction="vertical"
            auto-label-width
          >
            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-item field="equipNumber" label="设备编码：">
                  <a-input
                    placeholder="请输入设备编码"
                    size="large"
                    @change="changeEquipNumber"
                    v-model="currForm.list.equipNumber"
                    :disabled="type === 7 || type === 9"
                  />
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item field="number" label="保养单号：">
                  <a-input
                    placeholder="系统自动生成"
                    size="large"
                    v-model="currForm.list.number"
                    :disabled="type === 7 || type === 9 || type === 6"
                  />
                </a-form-item>
              </a-col>
            </a-row>

            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-item field="equipName" label="设备名称：">
                  <a-input
                    placeholder="根据设备编号，自动读取设备名称"
                    size="large"
                    v-model="currForm.list.equipName"
                    :disabled="type === 7 || type === 9 || type === 6"
                  />
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item field="workshop" label="所属车间：">
                  <a-input
                    placeholder="根据设备编号，自动读取所属车间"
                    size="large"
                    v-model="currForm.list.workshop"
                    :disabled="type === 7 || type === 9 || type === 6"
                  />
                </a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-item field="reportTime" label="保养周期：">
                  <a-input
                    :placeholder="type === 6 ? '-' : '请输入保养周期'"
                    size="large"
                    v-model="currForm.list.maintenanceCycleNum"
                    :disabled="type === 7 || type === 9 || type === 6"
                  >
                    <template #append>
                      {{ sumFn(currForm.list.maintenanceCycleUnit) }}
                    </template>
                  </a-input>
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item field="customerEmail" label="保养到期预警：">
                  <a-input
                    :placeholder="type === 6 ? '-' : '请输入保养周期'"
                    size="large"
                    v-model="currForm.list.maintenanceWarning"
                    :disabled="type === 7 || type === 9 || type === 6"
                  >
                    <template #append>
                      {{ sumFn(currForm.list.maintenanceCycleUnit) }}
                    </template>
                  </a-input>
                </a-form-item>
              </a-col>
            </a-row>

            <a-row :gutter="16">
              <a-col :span="12" class="dept">
                <a-form-item field="description" label="保养内容：">
                  <a-textarea
                    placeholder="请输入"
                    allow-clear
                    show-word-limit
                    :auto-size="{
                      minRows: 5,
                    }"
                    :max-length="300"
                    v-model="currForm.list.planContent"
                    :disabled="type === 7 || type === 9"
                  />
                </a-form-item>
              </a-col>
            </a-row>

            <a-row :gutter="16">
              <a-col :span="12" class="dept">
                <a-form-item field="description" label="保养标准：">
                  <a-textarea
                    placeholder="请输入"
                    allow-clear
                    show-word-limit
                    :auto-size="{
                      minRows: 5,
                    }"
                    :max-length="300"
                    v-model="currForm.list.planStandard"
                    :disabled="type === 7 || type === 9"
                  />
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <div v-if="type === 7 || type === 8 || type === 9 || type === 13">
          <h3>保养信息</h3>
          <!-- form表单设置auto-label-width开启自动标签宽度 -->
          <a-form
            ref="formRules"
            :model="currForm.list"
            direction="vertical"
            auto-label-width
          >
            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-item
                  field="maintenancePeopleId"
                  label="保养人："
                  required
                >
                  <a-select
                    placeholder="请选择人员"
                    allow-search
                    v-model="currForm.list.maintenancePeopleId"
                    :disabled="type === 9 || type === 13 || isTrue"
                    :options="personnelData.data"
                    @change="(e) => changeMaintenancePeopleName(e as number)"
                    :field-names="{
                      value: 'id',
                      label: 'name',
                    }"
                  >
                    <!-- <a-option :value="1">肖祖荣</a-option>
                    <a-option :value="2">Shanghai</a-option>
                    <a-option :value="3">Guangzhou</a-option>
                    <a-option :value="4" disabled>Disabled</a-option>0
                    <a-option :value="5">Shenzhen</a-option>
                    <a-option :value="6">Chengdu</a-option>
                    <a-option :value="7">Wuhan</a-option> -->
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <!-- <a-form-item field="maintainState" label="保养状态：" required>
                  <a-select
                    placeholder="请选择保养状态"
                    allow-search
                    v-model="currForm.list.maintainState"
                    :disabled="type === 9"
                  >
                    <a-option :value="1" disabled v-if="type !== 13">
                      待派工
                    </a-option>
                    <a-option :value="2" :disabled="type === 13">
                      待保养
                    </a-option>
                    <a-option :value="3" :disabled="type === 13">
                      进行中
                    </a-option>
                    <a-option :value="4" v-if="type !== 13">已保养</a-option>
                  </a-select>
                </a-form-item> -->
              </a-col>
            </a-row>

            <div v-if="type === 8 || type === 9 || type === 13">
              <a-row :gutter="16">
                <a-col :span="12">
                  <a-form-item
                    field="maintenanceStartTime"
                    label="保养开始时间："
                    required
                  >
                    <a-date-picker
                      style="width: 100%"
                      show-time
                      format="YYYY-MM-DD HH:mm:ss"
                      v-model="currForm.list.maintenanceStartTime"
                      value-format="x"
                      :disabled="type === 9"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="12">
                  <a-form-item
                    field="maintenanceEndTime"
                    label="保养结束时间："
                    required
                  >
                    <a-date-picker
                      style="width: 100%"
                      show-time
                      format="YYYY-MM-DD HH:mm:ss"
                      v-model="currForm.list.maintenanceEndTime"
                      :disabled="type === 9 || type === 13"
                      value-format="x"
                    />
                  </a-form-item>
                </a-col>
              </a-row>

              <a-row :gutter="16">
                <a-col :span="12">
                  <a-form-item field="isReplaceparts" label="是否更换备件：">
                    <a-radio-group
                      :default-value="2"
                      v-model="currForm.list.isChangeParts"
                      :disabled="type === 9 || type === 13"
                    >
                      <a-radio :value="true">是</a-radio>
                      <a-radio :value="false">否</a-radio>
                    </a-radio-group>
                  </a-form-item>
                </a-col>
                <a-col :span="12">
                  <a-form-item field="isNeedRepair" label="是否需要维修：">
                    <a-radio-group
                      :default-value="2"
                      v-model="currForm.list.isNeedRepair"
                      :disabled="type === 9 || type === 13"
                    >
                      <a-radio :value="true">是</a-radio>
                      <a-radio :value="false">否</a-radio>
                    </a-radio-group>
                  </a-form-item>
                </a-col>
              </a-row>

              <a-row :gutter="16">
                <a-col :span="12" class="dept">
                  <a-form-item
                    field="actualMaintenanceContent"
                    label="实际保养内容："
                  >
                    <a-textarea
                      placeholder="请输入"
                      allow-clear
                      show-word-limit
                      :auto-size="{
                        minRows: 5,
                      }"
                      v-model="currForm.list.actualMaintenanceContent"
                      :disabled="type === 9 || type === 13"
                    />
                  </a-form-item>
                </a-col>
              </a-row>

              <a-row :gutter="16">
                <a-col :span="12" class="dept">
                  <a-form-item
                    field="actualMaintenanceMeasure"
                    label="实际保养措施："
                  >
                    <a-textarea
                      placeholder="请输入"
                      allow-clear
                      show-word-limit
                      :auto-size="{
                        minRows: 5,
                      }"
                      v-model="currForm.list.actualMaintenanceMeasure"
                      :disabled="type === 9 || type === 13"
                    />
                  </a-form-item>
                </a-col>
              </a-row>
            </div>
          </a-form>
        </div>

        <div v-if="type === 10 || type === 11 || type === 12">
          <h3>保养计划</h3>
          <!-- form表单设置auto-label-width开启自动标签宽度 -->
          <a-form auto-label-width :model="currForm.list" direction="vertical">
            <div v-if="type === 12">
              <a-row :gutter="16">
                <a-col :span="12">
                  <a-form-item field="equipNumber" label="设备编码：">
                    <a-input
                      placeholder="自动读取设备编码"
                      size="large"
                      v-model="currForm.list.equipNumber"
                      disabled
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="12">
                  <a-form-item field="number" label="设备名称：">
                    <a-input
                      placeholder="系统自动生成"
                      size="large"
                      v-model="currForm.list.equipName"
                      disabled
                    />
                  </a-form-item>
                </a-col>
              </a-row>
              <a-row :gutter="16">
                <a-col :span="12">
                  <a-form-item field="workshop" label="所属车间：">
                    <a-input
                      placeholder="根据设备编号，自动读取所属车间"
                      size="large"
                      v-model="currForm.list.workshop"
                      disabled
                    />
                  </a-form-item>
                </a-col>
              </a-row>
            </div>
            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-item field="equipNumber" label="保养计划名称：">
                  <a-input
                    placeholder="请输入"
                    size="large"
                    v-model="currForm.list.planName"
                    :disabled="type === 12"
                  />
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item field="isSubcontracting" label="是否启用：">
                  <a-radio-group
                    :default-value="1"
                    v-model="currForm.list.isEnable"
                    :disabled="type === 12"
                  >
                    <a-radio :value="true">是</a-radio>
                    <a-radio :value="false">否</a-radio>
                  </a-radio-group>
                </a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter="16">
              <a-col :span="12">
                <a-form-item field="cycle" label="保养周期：">
                  <a-input-number
                    :min="1"
                    placeholder="请输入"
                    size="large"
                    v-model="currForm.list.maintenanceCycleNum"
                    hide-button
                    :disabled="type === 12"
                  >
                    <template #append>
                      <a-cascader
                        :options="emuipmentAge"
                        :style="{ width: '110px' }"
                        placeholder="请选择"
                        v-model="currForm.list.maintenanceCycleUnit"
                        :disabled="type === 12"
                      />
                    </template>
                  </a-input-number>
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item field="maintenanceWarning" label="保养到期预警：">
                  <a-input-number
                    :min="1"
                    placeholder="请输入到期预警时间"
                    size="large"
                    v-model="currForm.list.maintenanceWarning"
                    hide-button
                    :disabled="type === 12"
                  >
                    <template #append>
                      {{ sumFn(currForm.list.maintenanceCycleUnit) }}
                    </template>
                  </a-input-number>
                </a-form-item>
              </a-col>
            </a-row>

            <a-row :gutter="16">
              <a-col :span="12" class="dept">
                <a-form-item field="faultAnalysis" label="保养内容：">
                  <a-textarea
                    placeholder="请输入"
                    allow-clear
                    show-word-limit
                    :auto-size="{
                      minRows: 5,
                    }"
                    v-model="currForm.list.planContent"
                    :disabled="type === 12"
                  />
                </a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter="16">
              <a-col :span="12" class="dept">
                <a-form-item field="treatmentMeasures" label="保养标准：">
                  <a-textarea
                    placeholder="请输入"
                    allow-clear
                    show-word-limit
                    :auto-size="{
                      minRows: 5,
                    }"
                    v-model="currForm.list.planStandard"
                    :disabled="type === 12"
                  />
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </div>
      </div>
    </div>
  </a-modal>
</template>

<script lang="ts" setup>
  import { getToken } from '@/utils/auth';
  import { GetPersonnelinformation } from '@/api/home/System settings/personal-information/index';
  import dayjs from 'dayjs';
  import { cloneDeep, filter } from 'lodash';
  import { reactive, computed, ref, watchEffect, onMounted, watch } from 'vue';
  import { GetEquipmentList } from '@/api/equipment-management/equipment-list';
  import { Message } from '@arco-design/web-vue';

  const env = import.meta.env;
  const url = env.VITE_UPLOAD_URL;

  const props = withDefaults(
    defineProps<{
      visible: boolean; // 是否显示对话框
      form: any; // 传进来的数据
      /*
        1： 新增设备维修单
        2： 故障信息
        3： 新建维修任务(派工)
        4： 维修信息
        5： 设备修详情
        6： 新建设维备保养单
        7： 新建保养任务
        8： 保养内容
        9： 设备保养详情
        10: 新增保养计划
        11: 编辑保养计划
        12: 保养计划详情
        13： 保养内容(员工端开始保养)
        14: 开始检修(员工端)
        15: 编辑维修(员工端)
      */
      type: number;
      isTrue?: boolean;
    }>(),
    {
      visible: false,
    }
  );

  const emit = defineEmits(['update:visible', 'equipmentEmit']);
  const formRules = ref();
  const modalVisible = computed({
    get() {
      return props.visible;
    },
    set(visible: boolean) {
      emit('update:visible', visible);
    },
  });

  const currForm = reactive<any>({
    list: {
      number: '', // 维修单号
      equipNumber: '', // 设备编码
      equipName: '', // 设备名称
      workshop: '', // 所属车间
      reportTime: undefined, // 报修时间
      reporterId: null, // 报修人id
      repairerId: null, // 维修人id
      description: '', // 故障描述
      equipmentRepairPicture: [], // 图片上传
      repairState: 1, //维修状态 1.待派工；2.待维修；3.进行中；4.需委外.5.已委外；6.已完工
      repairBeginTime: '', // 维修开始时间
      repairEndTime: '', // 维修结束时间
      isReplaceparts: 1, // 是否更换部件(1是；2否)
      isSubcontracting: 1, // 是否需要委外(1是；2否)
      faultAnalysis: '', // 故障分析
      treatmentMeasures: '', // 维修措施
      equipmentId: null, // 设备id
      planName: '', // 保养计划名称
      isEnable: 1, // 是否启用
      planContent: '', // 保养内容
      maintenanceCycleNum: null, // 保养周期值
      maintenanceCycleUnit: null, // 保养周期单位
      planStandard: null, // 保养标准
      maintenanceWarning: null, // 保养到期预警时间
      equipmentMaintainPlan: {
        maintenanceCycleUnit: null,
        maintenanceCycleNum: null,
        maintenanceWarning: null,
        planName: '',
        planContent: '',
      },
    },
  });

  const emuipmentAge = [
    {
      value: 1,
      label: '时间',
      children: [
        { value: 3, label: '天' },
        { value: 2, label: '月' },
        { value: 1, label: '年' },
      ],
    },
    {
      value: 4,
      label: '数量',
      children: [
        { value: 4, label: '件' },
        { value: 5, label: '个' },
        { value: 6, label: '条' },
        { value: 7, label: '台' },
      ],
    },
  ];
  const sumFn = (sum: number) => {
    if (sum > 3) {
      return emuipmentAge[1].children.filter((ele) => ele.value === sum)[0]
        ?.label;
    } else {
      return emuipmentAge[0].children.filter((ele) => ele.value === sum)[0]
        ?.label;
    }
  };
  const equipmentList = reactive<any>({
    list: [],
  });

  // 获取设备列表数据
  const getEquipmentListData = async () => {
    try {
      const res = await GetEquipmentList({
        page: 0,
        size: -1,
      });
      equipmentList.list = res.content;
    } catch (error: unknown) {
      console.log(error);
    }
  };

  watch(modalVisible, (newVal, oldVal) => {
    // 如果newVal为true
    if (newVal) {
      const { form } = props;
      currForm.list = cloneDeep(form);
      currForm.list.equipmentRepairPicture?.forEach(
        (ele: any, index: number) => {
          ele.name = ele?.name ? null : `故障文件${index + 1}`;
          ele.url = ele.picture;
        }
      );
      if (props.type === 1 || props.type === 6) {
        getEquipmentListData();
        currForm.list.reportTime = dayjs().valueOf(); // 将时间改成时间戳形式
      }
    } else {
      emit('equipmentEmit', 1, 50);
    }
  });
  // 车间假数据
  const workshop = [
    { id: 1, value: '车间A' },
    { id: 2, value: '车间B' },
    { id: 3, value: '车间C' },
    { id: 4, value: '车间D' },
  ];

  let validNum = false; // 判断设备编码是否存在
  let isScrap = false;

  // 根据正确的设备编码读取其他数据
  const changeEquipNumber = async () => {
    validNum = false;
    isScrap = false;
    for (let i of equipmentList.list) {
      if (i.deviceCoding === currForm.list.equipNumber) {
        currForm.list.equipName = i.deviceName;
        currForm.list.workshop = workshop.filter(
          (ele) => Number(ele.id) === i.workshopId
        )[0]?.value;
        currForm.list.equipmentId = i.id;
        currForm.list.responsiblePersonName = i.responsiblePersonName;
        validNum = true;
        break;
      }
    }

    if (!validNum) {
      currForm.list.equipName = '';
      currForm.list.workshop = '';
      currForm.list.equipmentId = null;
      currForm.list.responsiblePersonName = '';
      Message.error('该设备编码不存在，请输入正确的设备编码！');
      return;
    }
    equipmentList.list.forEach(({ id, useState }: any) => {
      if (id === currForm.list.equipmentId) {
        if (useState === 7) {
          isScrap = true;
          Message.error('该设备已报废');
        }
      }
    });
  };

  // 保存
  const handleOk = async () => {
    if (!validNum && (props.type === 1 || props.type === 6)) {
      Message.error('请输入正确的设备编码！');
      return;
    }
    if (isScrap && (props.type === 1 || props.type === 6)) {
      Message.error('该设备已报废');
      return;
    }
    if (props.type === 8 || props.type === 9 || props.type === 13) {
      if (
        !currForm.list.maintenanceStartTime ||
        !currForm.list.maintenanceEndTime
      ) {
        if (!currForm.list.maintenanceStartTime) {
          Message.error('保养开始时间不能为空');
        } else Message.error('保养到期时间不能为空');
        return;
      }
    }

    if (await formRules.value?.validate()) {
      return;
    }
    emit('equipmentEmit', currForm.list, props.type);
  };

  // 取消
  const handleCancel = () => {
    emit('equipmentEmit', null, 22222);
    modalVisible.value = false;
  };
  // 上传文件
  const uploadFn = (flie: any) => {
    try {
      if (!currForm.list.equipmentRepairPicture) {
        currForm.list.equipmentRepairPicture = [];
      }
      currForm.list.equipmentRepairPicture.push({
        picture: flie.response.data.addr,
      });
    } catch (error: any) {
      Message.error(error.message);
      console.log(error);
    }
  };
  const personnelData: any = reactive({ data: [] });
  // 获取人员信息数据
  const Getuserlist = async () => {
    try {
      const res = await GetPersonnelinformation({
        page: 1,
        size: -1,
      });
      personnelData.data = res.content.data;
      personnelData.data.forEach((ele: any) => {
        ele.key = ele.id;
        ele.departmentName = ele.department.name;
        ele.positionName = ele.positionId.name;
      });
    } catch (err: unknown) {
      console.log(err);
    }
  };
  const changeMaintenancePeopleName = (e: number) => {
    const name = personnelData.data.filter((ele: any) => ele.id === e)[0]?.name;
    currForm.list.maintenancePeopleName = name;
  };
  Getuserlist();
</script>

<style scoped lang="less">
  .content {
    .dept {
      :deep(.arco-form-item-content-wrapper) {
        width: 200%;
      }
    }
  }
</style>
